package com.hpf.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpf.lease.common.config.CommonException;
import com.hpf.lease.common.result.ResultCodeEnum;
import com.hpf.lease.model.bean.GraphInfo;
import com.hpf.lease.model.bean.RoomInfo;
import com.hpf.lease.web.admin.dto.RoomSearchSubmitDto;
import com.hpf.lease.web.admin.dto.RoomSubmitDto;
import com.hpf.lease.web.admin.mapper.*;
import com.hpf.lease.web.admin.service.FileService;
import com.hpf.lease.web.admin.service.RoomInfoService;
import com.hpf.lease.web.admin.vo.RoomInfoVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author GOD
* @description 针对表【room_info(房间信息表)】的数据库操作Service实现
* @createDate 2024-08-29 15:00:50
*/
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
    implements RoomInfoService {
    @Resource
    RoomAttrValueMapper roomAttrValueMapper;
    @Resource
    RoomFacilityMapper roomFacilityMapper;
    @Resource
    RoomLabelMapper roomLabelMapper;
    @Resource
    RoomLeaseTermMapper roomLeaseTermMapper;
    @Resource
    RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Resource
    GraphInfoMapper graphInfoMapper;
    @Resource
    FileService fileService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateInfo(RoomSubmitDto submitDto) {
        long id = submitDto.getId() == null ? -1L : submitDto.getId();
        List<GraphInfo> graphList = null;
                RoomInfo one = this.getOne(Wrappers.lambdaQuery(RoomInfo.class)
                .eq(RoomInfo::getRoomNumber, submitDto.getRoomNumber())
                .eq(RoomInfo::getApartmentId, submitDto.getApartmentId()));
        if (one != null && one.getId().equals(id))
            throw new CommonException(ResultCodeEnum.ROOM_NAME_ERROR);
        if (id != -1L){
            roomFacilityMapper.deleteInfo(id);
            roomAttrValueMapper.deleteInfo(id);
            roomLabelMapper.deleteInfo(id);
            roomPaymentTypeMapper.deleteInfo(id);
            roomLeaseTermMapper.deleteInfo(id);
            graphList = graphInfoMapper.selectList(Wrappers
                    .lambdaQuery(GraphInfo.class)
                    .eq(GraphInfo::getItemType , 2)
                    .eq(GraphInfo::getItemId , id));
            graphInfoMapper.deleteRoomInfo(id);
        }
        RoomInfo roomInfo = new RoomInfo();
        BeanUtils.copyProperties(submitDto,roomInfo);
        this.saveOrUpdate(roomInfo);

        long nowId = roomInfo.getId();
        List<Long> facilityInfoIds = submitDto.getFacilityInfoIds();
        if (CollectionUtils.isEmpty(facilityInfoIds))
            roomFacilityMapper.insertInfo(nowId,facilityInfoIds);
        List<Long> attrValueIds = submitDto.getAttrValueIds();
        if (CollectionUtils.isEmpty(attrValueIds))
            roomAttrValueMapper.insertInfo(nowId , attrValueIds);
        List<Long> labelInfoIds = submitDto.getLabelInfoIds();
        if (CollectionUtils.isEmpty(labelInfoIds))
            roomLabelMapper.insertInfo(nowId , labelInfoIds);
        List<Long> leaseTermIds = submitDto.getLeaseTermIds();
        if (CollectionUtils.isEmpty(leaseTermIds))
            roomLeaseTermMapper.insertInfo(nowId , leaseTermIds);
        List<Long> paymentTypeIds = submitDto.getPaymentTypeIds();
        if (CollectionUtils.isEmpty(paymentTypeIds))
            roomPaymentTypeMapper.insertInfo(nowId , paymentTypeIds);
        List<GraphInfo> graphInfoList = submitDto.getGraphList();
        if (CollectionUtils.isEmpty(graphInfoList))
            graphInfoMapper.insertInfo(2 , nowId , graphInfoList );
        fileService.deleteImageBatch(graphList);
        return true;
    }

    @Override
    public Page<RoomInfoVo> getPageItem(RoomSearchSubmitDto submitDto) {
        Page<RoomInfoVo> page = new Page<>(submitDto.getCurrent(), submitDto.getSize());
        List<RoomInfoVo> pageItem = baseMapper.getPageItem(page,submitDto);
//         baseMapper.selectPage();
        page.setRecords(pageItem);
        return page;
    }

    @Override
    public RoomInfoVo getDetailById(long id) {
        return baseMapper.getDetailById(id);
    }
}




